    package com.code.day15;

    public class Day15Safe implements Runnable {
        static int ticket = 100;
        Object obj = new Object();

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                method();
            }
        }

        public static synchronized void method() {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "...买了第" + ticket + "张票");
                ticket--;
    //        }
    //    public void method() {
    //        synchronized (Day15SafeTest.class) {
    //            if (ticket > 0) {
    //                System.out.println(Thread.currentThread().getName() + "...买了第" + ticket + "张票");
    //                ticket--;
    //            }
            }
        }
    }